<?php

namespace App\Services;

use App\Models\Crm_User;
use App\Models\Crm_Wall;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Throwable;

class UserService
{
    public function getUserList($params)
    {
        $result = Crm_User::getFilter($params)->Nodel()->get();
        foreach ($result as $key => $value){
            $result[$key]['role_name'] = $value->hasOneRole->role_name;
            $result[$key]['is_super'] = $value->hasOneRole->is_super;
        }
        $result->data = $result->makeHidden(['hasOneRole']);
        return $result;
    }

    public function getList($params)
    {
        $result = Crm_User::getFilter($params)->Nodel()->paginate($params['pageSize']);
        foreach ($result as $key => $value){

            $result[$key]['role_name'] = $value->hasOneRole->role_name;
            $result[$key]['is_super'] = $value->hasOneRole->is_super;
        }
        $result->data = $result->makeHidden(['hasOneRole']);
        $list['list'] = $result->items();
        $list['total'] = $result->total();
        return $list;
    }

    public function addUser($params)
    {
        $user = Crm_User::where('agencyid',$params['aid'])->where('username',$params['username'])->first();
        if(!empty($user)){
            return ['code' => 2010,'status' => false];
        }
        try {
            // 开始数据库事务
            DB::beginTransaction();

            if(!empty($params['id'])){
                $res = Crm_User::where('id',$params['id'])->update([
                    'username' => $params['password'],
                    'plainpassword' => $params['password'],
                    'password' => Hash::make($params['password']),
                    'mobile' => $params['mobile'],
                    'avatar' => $params['avatar'],
                    'realname' => $params['realname'],
                    'weixin' => $params['weixin'],
                    'wxcode' => $params['wxcode'],
                    'roleid' => $params['roleid'],
                    'status' => $params['status'],
                    'is_servicer' => $params['is_servicer'],
                    'is_super' => $params['is_super']

                ]);
                if(!$res){
                    throw new \Exception("添加账号失败" );
                }
            }else{
                $res = Crm_User::create([
                    'agencyid' => $params['aid'],
                    'username' => $params['username'],
                    'plainpassword' => $params['password'],
                    'password' => Hash::make($params['password']),
                    'mobile' => $params['mobile'],
                    'avatar' => $params['avatar'],
                    'realname' => $params['realname'],
                    'weixin' => $params['weixin'],
                    'wxcode' => $params['wxcode'],
                    'roleid' => $params['roleid'],
                    'status' => $params['status'],
                    'is_servicer' => $params['is_servicer'],
                    'is_super' => $params['is_super']
                ]);
                if(!$res){
                    throw new \Exception("新增Crm_Gift礼物失败" );
                }
            }

            DB::commit();

            return ['code' => 200,'status' => true];
        }catch (Throwable $e) {
            // 如果在事务执行过程中发生异常，则回滚事务
            DB::rollBack();
            // 记录日志或处理错误
            Log::error("Database transaction failed: " . $e->getMessage());
            return ['code' => 2010,'status' => false];
        }

    }

    public function delUser($params)
    {
        $ids = explode(',',$params['ids']);
        try {
            // 开始数据库事务
            DB::beginTransaction();

            foreach ($ids as $id) {
                $res = Crm_User::find($id);
                $res->is_del = 'Y';
                if(!$res->save()){
                    throw new \Exception("删除Crm_User用户失败".$id);
                }
            }
            DB::commit();
            return true;
        }catch (Throwable $e) {
            // 如果在事务执行过程中发生异常，则回滚事务
            DB::rollBack();
            // 记录日志或处理错误
            Log::error("Database transaction failed: " . $e->getMessage());
            return false;
        }
    }
}
